package com.bigdata.demo.service;

import com.bigdata.demo.entity.CAList;
import com.bigdata.demo.entity.CRReport;
import com.bigdata.demo.entity.IER;
import com.bigdata.demo.repository.CAListRepository;
import com.bigdata.demo.repository.CRReportRepository;
import com.bigdata.demo.repository.IERRepository;
import com.bigdata.demo.web.dto.CADTO;
import com.bigdata.demo.web.dto.CRDTO;
import com.bigdata.demo.web.dto.IERDTO;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class CRReportServiceImpl implements CRReportService {

    @Autowired
    private CRReportRepository crReportRepository;

    @Autowired
    private CAListRepository caListRepository;


    @Autowired
    private IERRepository ierRepository;
    @Autowired
    private ModelMapper modelMapper;

    @Override
    public CRReport findById(String id_key_cr) {
        return crReportRepository.findById(id_key_cr);
    }

    @Override
    public ServiceResult<CRDTO> getCRInfo(String id_key_cr) {

        CRReport crReport=findById(id_key_cr);
        CRDTO crdto=new CRDTO();
        modelMapper.map(crReport,crdto);
        return ServiceResult.of(crdto);

    }

    @Override
    public ServiceMultiResult<CRDTO> getKKSCrs(String kks) {
        List<CRReport> crs=crReportRepository.findCRReportsByCr_equipmentOrderByCr_dateDesc(kks);

        List<CRDTO> crdtos=new ArrayList<>();
        for (CRReport cr:crs){
            CRDTO crdto=new CRDTO();
            modelMapper.map(cr,crdto) ;
            crdtos.add(crdto);
        }

        return new ServiceMultiResult<>(crdtos.size(), crdtos);
    }

    @Override
    public ServiceResult<CADTO> getCAInfo(String cano) {

      CAList caList= caListRepository.findById(cano).get();

      CADTO cadto=new CADTO();
      modelMapper.map(caList,cadto);

        return ServiceResult.of(cadto);
    }

    @Override
    public ServiceResult<IERDTO> getIERInfo(String erno) {

        IER ier= ierRepository.findById(erno).get();

        IERDTO ierdto=new IERDTO();
        modelMapper.map(ier,ierdto);

        return ServiceResult.of(ierdto);
    }


}
